package business.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import model.T_NovelContent;
import model.V_NovelContent;
import business.basc.BaseDAO;
import business.basc.BaseDaoImpl;
import business.dao.NovelContentDAO;

public class NovelContentDAOImpl implements NovelContentDAO {
	private BaseDAO bdao =  new BaseDaoImpl();
	@Override
	public int produceValidSortId(int novelid) {
		int ordernum = 0;
		String sql = "select max(ordernum) from T_NovelContent where novelid = ?";
		Object [] para = {novelid};
		ResultSet rs = bdao.select(sql,para);
		try {
			if (rs.next()) {
				ordernum = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			bdao.close();
		}
		return ordernum+1;
	}

	@Override
	public int addContent(T_NovelContent ncontent) {
		String procName ="up_addNovelContent(?,?,?,?,?)";
		Object para[]={ncontent.getNovelid(),ncontent.getChaptername(),ncontent.getTextcontent(),
				ncontent.isCharge(),ncontent.getOrdernum()};
		return (Integer)bdao.executeProduce(procName,para);
	}

	@Override
	public boolean deleteContent(int chapterid) {
		String procName = "up_deleteNovelContent(?)";
		Object [] para = {chapterid};
		int row  = (Integer) bdao.executeProduce(procName,para);
		if (row>0) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean deleteContentsBelongNovel(int novelid) {
		String sql = "delete from T_NovelContent where novelid = ?";
		Object[] para = {novelid};
		int row = bdao.delete(sql, para);
		if(row > 0 ){
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean updateNovelContent(T_NovelContent ncontent) {
		String sql = "update T_NovelContent set novelid =?, chaptername=?,"
				+ "textcontent=?,charge=? where chapterid=?";
		Object[] para = {ncontent.getNovelid(),ncontent.getChaptername(),
				ncontent.getTextcontent(),ncontent.isCharge(),ncontent.getChapterid()};
		int row =bdao.update(sql,para);
		if (row>0) {
			return true;
		}
		else {
			return false;
		}
		
	}

	@Override
	public V_NovelContent getContnentByName(int chapterid) {

		String sql = "select * from V_NovelContent where chapterid =?";
		Object [] para= {chapterid};
		ResultSet rSet = bdao.select(sql,para);
		V_NovelContent vnc = new V_NovelContent(rSet);
		
		return vnc;
	}

	@Override
	public T_NovelContent getTContnentByName(int chapterid) {
		String sql = "select * from T_NovelContent where chapterid =?";
		Object [] para= {chapterid};
		ResultSet rSet = bdao.select(sql,para);
		T_NovelContent tnc = new T_NovelContent(rSet);
		
		return tnc;
	}
	
	@Override
	public int getNovelContentByNum(int novelid){
		int count = 0;
		String sql = "select count(*) from V_NovelContent where novelid =?";
		Object [] para= {novelid};
		ResultSet rSet = bdao.select(sql,para);
		try {
			if (rSet.next()) {
				count = rSet.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
		
	}
	@Override
	public List<V_NovelContent> getContentListByNovelName(int novelid){
		List<V_NovelContent> list =null;
		String sql = "select * from V_NovelContent where novelid =?";
		Object[] para = {novelid};
		ResultSet rs = bdao.select(sql,para);
		if (rs!=null) {
			list = V_NovelContent.toList(rs);
		}
		
		return list;
	}
	
	public  V_NovelContent getNewNovelContentByNovelName(int novelid){
		int contentid=0;
		String sql = "select max(chapterid) from V_NovelContent where novelid = ?";
		Object [] pareObjects = {novelid};
		ResultSet rs = bdao.select(sql,pareObjects);
		try {
			if (rs.next()) {
				contentid = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			bdao.close();
		}
		int chapterid = contentid;
		String sql2 = "select * from V_NovelContent where chapterid = ?";
		Object [] pareObjects2 = {chapterid};
		ResultSet rsSet = bdao.select(sql2,pareObjects2);
		V_NovelContent vnc = new V_NovelContent(rsSet);
		return vnc;
	}

	@Override
	public List<V_NovelContent> getRecentUpdates() {
		String sql = "select * from V_NovelContent order by chapterid desc";
		ResultSet rs = bdao.select(sql);
		List<V_NovelContent> list = V_NovelContent.toList(rs);
		bdao.close();
		return list;
	}
	
	@Override
	public V_NovelContent getNovelContentAddCount(int ordernum,int novelid){
		
		int ordercount = ordernum + 1;
		String sql = "select * from V_NovelContent where ordernum = ? and novelid=?";
		Object[] para={ordercount,novelid};
		ResultSet rs = bdao.select(sql,para);
		V_NovelContent vnc = new V_NovelContent(rs);
		return vnc;
		
	}
	
	@Override
	public V_NovelContent getNovelContentFristCount(int ordernum,int novelid){
		int ordercount = ordernum - 1;
		String sql = "select * from V_NovelContent where ordernum = ? and novelid=?";
		Object[] para={ordercount,novelid};
		ResultSet rs = bdao.select(sql,para);
		V_NovelContent vnc = new V_NovelContent(rs);
		return vnc;
		
	}
}
